<template>
  <div class="box">
    <el-alert
      title="注意：只允许为第三级分类设置相关参数！"
      type="warning"
      show-icon
      style="margin-bottom: 20px"
    >
    </el-alert>
    <!-- 级联选择器 -->
    <div>
      选择商品分类
      <el-cascader
        v-model="value"
        :options="tableData"
        :props="{
          expandTrigger: 'hover',
          label: 'cat_name',
          value: 'cat_id',
        }"
        @change="handleChange"
      ></el-cascader>
    </div>
    <!-- 标签页 -->
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="动态参数" name="many">
        <!-- 添加参数 -->
        <el-button
          type="primary"
          :disabled="value.length != 3 ? true : false"
          size="small"
          style="margin-bottom: 10px"
          @click="dialogVisible = true"
          >添加参数</el-button
        >
        <el-table :data="tableData1" style="width: 100%" border>
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-tag
                v-for="(tag, i) in props.row.attr_vals"
                :key="tag.attr_name"
                closable
                @close="handleClose(props.row, i)"
              >
                {{ tag }}
              </el-tag>
              <el-input
                class="input-new-tag"
                v-if="props.row.inputVisible"
                v-model="props.row.inputValue"
                ref="aa"
                size="small"
                @keyup.enter.native="handleInputConfirm(props.row)"
              >
              </el-input>
              <el-button
                v-else
                class="button-new-tag"
                size="small"
                @click="showInput(props.row)"
                >+ New Tag</el-button
              >
            </template>
          </el-table-column>
          <el-table-column type="index"> </el-table-column>
          <el-table-column label="参数名称" prop="attr_name"> </el-table-column>
          <el-table-column fixed="right" label="操作">
            <template slot-scope="scope">
              <el-button plain icon="el-icon-edit" @click="bj(scope.row)"
                >编辑</el-button
              >
              <el-button
                type="danger"
                icon="el-icon-delete"
                @click="del(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="静态属性" name="only">
        <!-- 添加参数 -->
        <el-button
          type="primary"
          :disabled="value.length != 3 ? true : false"
          size="small"
          style="margin-bottom: 10px"
          @click="dialogVisible = true"
          >添加属性</el-button
        >
        <el-table :data="tableData1" style="width: 100%" border>
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-tag
                v-for="tag in props.row.attr_vals"
                :key="tag.attr_name"
                closable
              >
                {{ tag }}
              </el-tag>
              <el-input
                class="input-new-tag"
                v-if="props.row.inputVisible"
                v-model="props.row.inputValue"
                ref="aa"
                size="small"
                @keyup.enter.native="handleInputConfirm(props.row)"
                @blur="handleInputConfirm(props.row)"
              >
              </el-input>
              <el-button
                v-else
                class="button-new-tag"
                size="small"
                @click="showInput(props.row)"
                >+ New Tag</el-button
              >
            </template>
          </el-table-column>
          <el-table-column type="index"> </el-table-column>
          <el-table-column label="属性名称" prop="attr_name"> </el-table-column>
          <el-table-column fixed="right" label="操作">
            <template slot-scope="scope">
              <el-button plain icon="el-icon-edit" @click="bj(scope.row)"
                >编辑</el-button
              >
              <el-button
                type="danger"
                icon="el-icon-delete"
                @click="del(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <!-- 添加模态框 -->
    <el-dialog title="添加动态参数" :visible.sync="dialogVisible" width="40%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="动态参数" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="qAddParams">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑模态框 -->
    <el-dialog title="修改动态参数" :visible.sync="dialogVisible1" width="40%">
      <el-form
        :model="ruleForm1"
        :rules="rules1"
        ref="ruleForm1"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="动态参数" prop="name">
          <el-input v-model="ruleForm1.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="qEditParams">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "",
  components: {},
  data() {
    return {
      value: [],
      activeName: "many", //获取动态或静态参数
      id: "", //选中分类的id
      tableData: [], //选择商品分类的参数
      tableData1: [], //表格参数
      dialogVisible: false, //添加参数模态框开关
      dialogVisible1: false, //编辑参数模态框开关
      attr_id: "",
      // 添加表单
      rules: {
        name: [{ required: true, message: "请输入动态参数", trigger: "blur" }],
      },
      ruleForm: {
        name: "",
      },
      // 修改表单
      rules1: {
        name: [{ required: true, message: "请输入动态参数", trigger: "blur" }],
      },
      ruleForm1: {
        name: "",
        id: "",
      },
    };
  },
  created() {
    this.getTypeList();
  },
  mounted() {},
  methods: {
    // 获取所有分类列表
    getTypeList() {
      this.$store.dispatch("getgoods").then((res) => {
        if (res.data.meta.status == 200) {
          this.tableData = res.data.data;
        }
      });
    },
    // 获取选中的分类
    handleChange(val) {
      this.id = this.value[this.value.length - 1];
      if(val.length==3){
        this.getList();
      }
      else{
        this.$message.error("请选择三级分类")
      }
      
    },
    // 获取两个表格的数据
    getList() {
      this.$store
        .dispatch("jd", { id: this.id * 1, sel: this.activeName })
        .then((res) => {
          if (res.meta.status == 200) {
            this.$message.success(res.meta.msg);
            // 通过深拷贝获得请求的数据，再添加自己的值和input框的状态
            let data = JSON.parse(JSON.stringify(res.data));
            data.forEach((item) => {
              item.attr_vals = item.attr_vals ? item.attr_vals.split(",") : [];
              item.inputValue = "";
              item.inputVisible = false;
            });
            this.tableData1 = data;
          }
        });
    },
    // 显示input框并触发input获取焦点事件
    showInput(val) {
      val.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.aa.$refs.input.focus();
      });
    },
    // 敲回车添加tag
    handleInputConfirm(a) {
      // 获取当前行分类id
      this.attr_id = a.attr_id;
      if (a.inputValue.trim().length == 0) {
        a.inputValue = "";
        a.inputVisible = false;
        return;
      }
      // 把内容添加到数组中
      a.attr_vals.push(a.inputValue.trim());
      // 把需要传递的值写成对象
      var obj = {
        id: this.id, //当前选中的第三级id
        attr_id: a.attr_id, //当前行的参数id
        attr_name: a.attr_name, //当前行的属性名
        attr_sel: this.activeName, //静态或动态
        attr_vals: a.attr_vals.join(","), //接口文档要求值是一个字符串
      };
      // 添加属性
      this.addParamsHandler(obj);
    },
    // 添加tag的接口
    addParamsHandler(obj) {
      this.$store.dispatch("addParams", obj).then((res) => {
        if (res.meta.status == 200) {
          this.$message.success(res.meta.msg);
          this.getList();
        }
      });
    },
    // 添加参数
    qAddParams() {
      let obj = {
        id: this.id,
        attr_name: this.ruleForm.name,
        attr_sel: this.activeName,
      };
      this.$store.dispatch("qAddParams", obj).then((res) => {
        if (res.meta.status) {
          this.$message.success(res.meta.msg);
          this.getList();
        }
      });
    },

    // 编辑
    bj(row) {
      this.dialogVisible1 = true;
      // 数据回流
      this.ruleForm1.name = row.attr_name;
      this.ruleForm1.id = row.attr_id;
    },
    // 确认修改
    qEditParams() {
      let obj = {
        id: this.id,
        attrid: this.ruleForm1.id,
        attr_name: this.ruleForm1.name,
        attr_sel: this.activeName,
      };
      this.$store.dispatch("qEditParams", obj).then((res) => {
        if (res.meta.status == 200) {
          this.dialogVisible1 = false;
          this.$message.success(res.meta.msg);
          this.getList();
        }
      });
    },

    // 删除
    del(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let obj = { id: this.id, attrid: row.attr_id };
          this.$store.dispatch("delParams1", obj).then((res) => {
            if (res.meta.status == 200) {
              this.$message.success(res.meta.msg);
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // tab切换触发
    handleClick(tab, event) {
      if (this.value.length == 3) {
        this.getList();
      } else {
        this.$message({
          message: "请选择三级列表",
          type: "error",
        });
      }
    },
    // 删除tag标签
    handleClose(a, i) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 对数组进行删除
          a.attr_vals.splice(i, 1);
          // 需要给后台传字符串
          let str = a.attr_vals.join(",");
          // 后台请求的参数
          let obj = {
            id: this.id,
            attrid: a.attr_id,
            attr_sel: this.activeName,
            attr_name: a.attr_name,
            attr_vals: str,
          };
          // 发送修改参数请求
          this.$store.dispatch("delTag", obj).then((res) => {
            if (res.meta.status == 200) {
              this.$message.success(res.meta.msg);
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped lang='scss'>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
